// pages/seeksService/seeksService.js
const ajax = require('../../../utils/quest.js');
const util = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navScrollLeft: 10,
    navListL: [],
    storeAdvList: [{
        image: 'https://cdn.uviewui.com/uview/swiper/1.jpg',
        title: '昨夜星辰昨夜风，画楼西畔桂堂东'
      },
      {
        image: 'https://cdn.uviewui.com/uview/swiper/2.jpg',
        title: '身无彩凤双飞翼，心有灵犀一点通'
      },
      {
        image: 'https://cdn.uviewui.com/uview/swiper/3.jpg',
        title: '谁念西风独自凉，萧萧黄叶闭疏窗，沉思往事立残阳'
      }
    ],
    shopCategoryList: [],
    recIndexTab: 0,
    recommendListData: [],
    limit: 4,
    page: 1,
    dataList: [],
    ifHaveMore: false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNavList()
    this.getTree()
    this.getRecommendList()
  },
  getRecommend: function (e) {
    this.setData({
      recIndexTab: e.currentTarget.dataset.id,
      dataList: [],
      recommendListData: [],
      page: 1
    })
    this.getRecommendList()
  },
  bandMore: function () {
    wx.navigateTo({
      url: '/pages/seeksService/choice/index',
    })
  },
  getShopTypes: function (e) {
    const id = e.currentTarget.dataset.id.id
    const name = e.currentTarget.dataset.id.categoryName
    wx.navigateTo({
      url: '/pages/seeksService/searchService/index?id=' + id + '&name=' + name,
    })
  },
  getNavList: function () {
    ajax.sendRequest('/category/listParentCategory', 'get').then(res => {
      const {
        categoryVOList
      } = res.data
      this.setData({
        navListL: categoryVOList
      })
    })
  },
  getTree: function () {
    ajax.sendRequest('/category/getCategoryTree', 'get').then(res => {
      const {
        categoryVOList
      } = res.data
      const entire = {
        id: 0,
        categoryName: '全部'
      }
      categoryVOList.unshift(entire)
      this.setData({
        shopCategoryList: categoryVOList
      })
    })
  },
  getRecommendList() {
    const {
      page,
      limit,
      recIndexTab
    } = this.data
    const data = {
      page: page,
      limit: limit,
      categoryId: recIndexTab
    }
    let params = util.setQueryConfig(data)
    ajax.sendRequest('/findService/listProvide?' + params, 'get').then(res => {
      const dataList = this.data.dataList
      console.log("1", dataList);
      const list = res.data.page.list
      console.log("2", list);
      if (list.length) {
        for (var i = 0; i < list.length; i++) {
          dataList.push(res.data.page.list[i])
        }
      }
      console.log("3", dataList);

      this.setData({
        recommendListData: dataList,
        ifHaveMore: true,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('到底了');
    const {
      ifHaveMore
    } = this.data
    if (ifHaveMore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getRecommendList()
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: "none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})